/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.statementView;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.ViewCommodityOrderReturnSummary;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.commons.util.exportExcel.CommodityOrderReturnExportUtil;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonPrint;
import com.ruipengkj.component.jpanel.RPJPanelStatementTop;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.ViewCommoditySaleReturnSummaryModel;
import com.ruipengkj.service.ViewCommodityOrderReturnSummaryService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.orderView.OrderDetailDialog;

/**
 * 商品销售退货汇总表
 * 黄盛强
 */
public class StatementCommoditySalesReturnSummary extends BasePagePanel<ViewCommodityOrderReturnSummary> implements Runnable{
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent commodityNameTF;//商品名称
	
	private JButton selectButton;//选择
//	private JButton printButton; //打印
	private JButton exportButton; //导出
//	private JButton exitButton; //退出
	
	private JTable table;//商品销售汇总表
	private JScrollPane tableJsp;//
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<ViewCommodityOrderReturnSummary> pageView;//翻页主键
	
	private ViewCommoditySaleReturnSummaryModel tableModel;
	
	private ViewCommodityOrderReturnSummaryService viewCommodityOrderReturnSummaryService;
	public StatementCommoditySalesReturnSummary() {
		viewCommodityOrderReturnSummaryService = BuildSpringApplication.getInstance().getViewCommodityOrderReturnSummaryServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	
	private void init() {
		pageView = new PageView<ViewCommodityOrderReturnSummary>();
		tableModel = new ViewCommoditySaleReturnSummaryModel();
		table = uiFactory.createRPJTable(tableModel, null, this, 30, JLabel.CENTER);;
		tableJsp = new JScrollPane(table);
		
		this.setOpaque(true);
		this.setBackground(new Color(247, 252, 255));
		table.setOpaque(false);
		tableJsp.setOpaque(false);
		tableJsp.getViewport().setOpaque(false); 
		this.add(uiFactory.createFlowJPanel(), BorderLayout.WEST);
		this.add(uiFactory.createFlowJPanel(), BorderLayout.EAST);
		
		this.add(this.topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(this.tailPanel(), BorderLayout.SOUTH);
		
		//排序
		TableRowSorter<ViewCommoditySaleReturnSummaryModel> tableRowSorter = new TableRowSorter<ViewCommoditySaleReturnSummaryModel>(
				tableModel);
		table.setRowSorter(tableRowSorter);
	}
	
	private JPanel topPanel() {
		JPanel jp1 = new RPJPanelStatementTop();
		commodityNameTF = new JLabelJTextFieldComponent("商品名称：", 30);
		selectButton = new RPJButtonBlue("查询");
		jp1.add(commodityNameTF);
		jp1.add(selectButton);
		return jp1;
	}
	
	private JPanel tailPanel() {
		JPanel tail_panel = uiFactory.createBorderJPanel();
		
		JPanel jp = uiFactory.createGridJPanel(1, 2);
		JPanel jp2 = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		exportButton = new RPJButtonPrint("导 出");
		jp2.add(uiFactory.createJLabel("       "));
		jp2.add(exportButton);
		
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		Util.setSize(pagingComponent, 80, 24);
		jp.add(jp2);
		jp.add(pagingComponent);
		
		tail_panel.add(jp);
		tail_panel.add(uiFactory.createFlowJPanel(), BorderLayout.NORTH);
		tail_panel.add(uiFactory.createFlowJPanel(), BorderLayout.SOUTH);
		return tail_panel;
	}
	
	private void initListener(){
		selectButton.addActionListener(this);
		exportButton.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==selectButton){
			this.termQuery(1);
		} else if(e.getSource()==exportButton){
			//导出
			FileDialog fileDialog = new FileDialog(new JDialog(), "文件导出",FileDialog.SAVE);
			fileDialog.setVisible(true);
			String fileDir = fileDialog.getDirectory();
			String fileName = fileDialog.getFile();
			if (fileDir != null && !fileDir.equals("") && fileName != null&& !fileName.equals("")) {
				if (!fileName.toLowerCase().endsWith(".xls")) {
					if (fileName.lastIndexOf(".") != -1) {
						fileName = fileName.substring(0, fileName.lastIndexOf("."))+ ".xls";
					} else {
						fileName = fileName + ".xls";
					}
				}
				try {
					List<ViewCommodityOrderReturnSummary> list = viewCommodityOrderReturnSummaryService.findExportService(this.queryTerm());
					if (list != null && list.size() != 0) {
						exportButton.setEnabled(false);
						CommodityOrderReturnExportUtil.writeExcel(fileDir + fileName,list);
						JOptionPane.showMessageDialog(null, "导出成功");
					} else {
						JOptionPane.showMessageDialog(null, "数据为空，导出失败");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "导出失败");
				}
				exportButton.setEnabled(true);
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == table && e.getClickCount() == 2) {
			OrderDetailDialog orderDetailDialog = new OrderDetailDialog(null, "销售详细", true);
			orderDetailDialog.showDialog((String)table.getValueAt(table.getSelectedRow(), 0));
		} 
	}
	
	private ViewCommodityOrderReturnSummary queryTerm(){
		ViewCommodityOrderReturnSummary viewCommodityOrderReturnSummary=new ViewCommodityOrderReturnSummary();
		viewCommodityOrderReturnSummary.setCommodityName(commodityNameTF.getText().getText());
		return viewCommodityOrderReturnSummary;
	}
	
	@Override
	protected void termQuery(final int currentPage) {
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<ViewCommodityOrderReturnSummary> pageModel=viewCommodityOrderReturnSummaryService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),queryTerm());
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						TabelUtil.clearTableRowSorter(table);
						tableModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						tableModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<ViewCommodityOrderReturnSummary> getPageView() {
		return pageView;
	}
	@Override
	public void run() {
		this.termQuery(1);
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
